package com.sgb.kjwl.view.ui.activity.home;

import androidx.databinding.DataBindingUtil;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentSearchBinding;
import com.sgb.kjwl.di.home.DaggerHomeFragComponent;
import com.sgb.kjwl.di.home.HomeFragModule;
import com.sgb.kjwl.model.entity.RecommendEntity;
import com.sgb.kjwl.model.entity.StoreEntity;
import com.swgk.core.event.EventConstant;
import com.sgb.link.event.LeaseEvent;
import com.sgb.kjwl.view.ui.activity.self.shop.ShopGoodsDetailActivity;
import com.sgb.kjwl.view.ui.adapter.home.SearchGoodsAdapter;
import com.sgb.kjwl.viewmodel.home.SearchFragmentModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.xrecyclerview.XRecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import javax.inject.Inject;

/**
 * 首页-商品材料的搜索
 *
 * @author Administrator
 */
public class SearchFragment extends AppFragment {
    public static final String TAG = "SearchFragment";
    public static final String KEY_DATATYPE = "data_type";
    private FragmentSearchBinding binding;

    @Inject
    SearchFragmentModel viewModel;

    private String searchText = "";

    private LinearLayoutManager linearLayoutManager;
    private SearchGoodsAdapter searchGoodsAdapter;

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerHomeFragComponent.builder()
                .appComponent(appComponent)
                .homeFragModule(new HomeFragModule(this))
                .build()
                .inject(this);
    }

    public static SearchFragment newInstance() {
        SearchFragment fragment = new SearchFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_search, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        initData();
    }

    private void initData() {
        binding.recyclerView.setVisibility(View.GONE);
        binding.emptyView.blankView.setVisibility(View.GONE);
        linearLayoutManager = new LinearLayoutManager(getActivity());
        binding.recyclerView.setLayoutManager(linearLayoutManager);
        searchGoodsAdapter = new SearchGoodsAdapter(getActivity(), null);
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.recyclerView.setAdapter(searchGoodsAdapter);
        searchGoodsAdapter.setOnItemClick(new AdapterOnItemClick<RecommendEntity>() {
            @Override
            public void onItemClick(RecommendEntity recommendEntity, int position) {
                ShopGoodsDetailActivity.start(getActivity(), recommendEntity.getId(), "", recommendEntity.getSkuid());
            }
        });
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.searchCategories(SearchActivity4.SEARCHTYPE_GOODS, searchText, false);
            }

            @Override
            public void onLoadMore() {
                viewModel.searchCategories(SearchActivity4.SEARCHTYPE_GOODS, searchText, true);
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvent(LeaseEvent event) {
        searchText = (String) event.getMessageData();
        if (event.getMessageTag() == EventConstant.SEARCH_GOOODS) {
            if (this.getActivity() instanceof SearchActivity4) {
                viewModel.searchCategories(SearchActivity4.SEARCHTYPE_GOODS, searchText, false);
            }
            EventBus.getDefault().removeStickyEvent(event);
        }
    }

    public void refresh(List<RecommendEntity> recommendEntityList, List<StoreEntity> storeEntityList, boolean isLast) {
        binding.emptyView.blankView.setVisibility(View.GONE);
        binding.recyclerView.setVisibility(View.VISIBLE);
        binding.recyclerView.setLoadingMoreEnabled(true);
        searchGoodsAdapter.setDatas(recommendEntityList);
        binding.recyclerView.refreshComplete();
        if (isLast) {
            binding.recyclerView.setLoadingMoreEnabled(false);
        }
    }

    public void empty() {
        binding.recyclerView.refreshComplete();
        binding.recyclerView.setVisibility(View.GONE);
        binding.emptyView.blankView.setVisibility(View.VISIBLE);
        searchGoodsAdapter.setDatas(null);
    }

    public void loadMore(List<RecommendEntity> recommendEntityList, List<StoreEntity> storeEntityList, boolean isLast) {
        searchGoodsAdapter.addDatas(recommendEntityList);
        binding.recyclerView.loadMoreComplete();
        if (isLast) {
            binding.recyclerView.setLoadingMoreEnabled(false);
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
